package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;


/**
 * Test cases for ReseacherActionBean.
 * 
 * @author Ed Kim
 * @author Jon Lao
 * @author JianFei Liao
 * @author Jared Sunouchi
 */
public class TestResearcherActionBean {

  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  /** A string representing the homepage title */
  private static final String HOMEPAGE_TITLE = "MyISERN 2.0 - Home";
  /** A string representing the login page title */
  private static final String LOGIN_TITLE = "MyISERN 2.0 - Login";
  /** A string represent the username attribute */
  private static final String USERNAME_FIELD = "username";
  /** A string represent the password attribute */
  private static final String PASSWORD_FIELD = "password";
  /** A string representing the user name */
  private static final String USERNAME = "username";
  /** A string representing the password */
  private static final String PASSWORD = "password";
  /** A string representing the collaboration form */
  private static final String RESEARCHER_FORM = "ResearcherForm";
  /** A string to represent the name field */
  private static final String NAME = "name";
  /** A string to represent the add field */
  private static final String ADD = "add";
  
  
  /**
   * Tests adding of researcher, happy trail style.
   * 
   * @throws Exception if error occurs.
   */
  @Test
  public void testAddResearcherActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Clicking link without login", LOGIN_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(USERNAME_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Logged in", HOMEPAGE_TITLE, response.getTitle());
    
    WebLink link = response.getLinkWithID("addResearcher");
    response = link.click();
    assertEquals("Adding researcher", "MyISERN 2.0 - Add Researcher", response.getTitle());
    
    WebForm addResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    WebRequest addResearcherRequest = addResearcherForm.getRequest("reset");
    addResearcherRequest.setParameter(NAME, "Testing Clear");
    response = conversation.getResponse(addResearcherRequest);
    
    addResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    addResearcherRequest = addResearcherForm.getRequest(ADD);
    response = conversation.getResponse(addResearcherRequest);
    
    addResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    addResearcherRequest = addResearcherForm.getRequest(ADD);
    
    addResearcherRequest.setParameter("name", "Test Name");
    addResearcherRequest.setParameter("organization", "Test Organization");
    addResearcherRequest.setParameter("email", "Test@TestEmail.com");
    addResearcherRequest.setParameter("pictureLink", "http://Testpicturelink.jpg");
    addResearcherRequest.setParameter("bioStatement", "Test Bio-statement");
    
    response = conversation.getResponse(addResearcherRequest);
    
    link = response.getLinkWithID("addResearcher");
    response = link.click();
    assertEquals("Adding researcher", "MyISERN 2.0 - Add Researcher", response.getTitle());
    
    addResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    addResearcherRequest = addResearcherForm.getRequest(ADD);
    addResearcherRequest.setParameter(NAME, "");
    response = conversation.getResponse(addResearcherRequest);
    
    addResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    addResearcherRequest = addResearcherForm.getRequest(ADD);
    
    addResearcherRequest.setParameter("name", "Test Name");
    addResearcherRequest.setParameter("organization", "Test Organization");
    addResearcherRequest.setParameter("email", "Test@TestEmail.com");
    addResearcherRequest.setParameter("pictureLink", "http://Testpicturelink.jpg");
    addResearcherRequest.setParameter("bioStatement", "Test Bio-statement");
    
    response = conversation.getResponse(addResearcherRequest);
    assertEquals("Went to edit page", "MyISERN 2.0 - Edit Researcher", response.getTitle());
    
    link = response.getLinkWithID("addResearcher");
    response = link.click();
    assertEquals("Adding researcher", "MyISERN 2.0 - Add Researcher", response.getTitle());
    
    addResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    addResearcherRequest = addResearcherForm.getRequest("cancel");
    response = conversation.getResponse(addResearcherRequest);
    
    link = response.getLinkWithID("logout");
    link.click();
    response = conversation.getCurrentPage();
    assertEquals("Logged out", LOGIN_TITLE, response.getTitle());
  }
  
  /**
   * Tests editing of researcher, happy trail style.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testEditResearcherActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Clicking link without login", LOGIN_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(USERNAME_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Logged in", HOMEPAGE_TITLE, response.getTitle());
    
    WebLink link = response.getLinkWithID("editResearcher");
    response = link.click();
    assertEquals("Checking edit researcher page retrieval", "MyISERN 2.0 - Edit Researcher",
        response.getTitle());
    
    WebForm editResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    WebRequest editResearcherRequest = editResearcherForm.getRequest();
    
    editResearcherRequest.setParameter(NAME, "Test Name");
    
    response = conversation.getResponse(editResearcherRequest);
    
    editResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    editResearcherRequest = editResearcherForm.getRequest("edit");
    
    editResearcherRequest.setParameter("bioStatement", "Test Bio-Statement Edited");
    
    response = conversation.getResponse(editResearcherRequest);
    
    editResearcherForm = response.getFormWithID(RESEARCHER_FORM);
    editResearcherRequest = editResearcherForm.getRequest("remove");
    response = conversation.getResponse(editResearcherRequest);
    
    link = response.getLinkWithID("logout");
    link.click();
    response = conversation.getCurrentPage();
    assertEquals("Logged out", LOGIN_TITLE, response.getTitle());
  }  
}
